package com.kinyx.framework.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;

import javax.util.XArrayList;

import org.junit.Assert;
import org.junit.Test;

public class KListUtils_Tester_join {

	@Test
	public void test1() {
		Assert.assertNull(KListUtils.join(null, null, null));
	}
	@Test
	public void test2() {
		Assert.assertEquals("", KListUtils.join(new ArrayList<>(), null, null));
	}

	final List<String> list1 = new XArrayList<String>().xAdd("1");
	final List<String> list2 = new XArrayList<String>().xAdd("1").xAdd("2");
	final List<String> list3 = new XArrayList<String>().xAdd("1").xAdd("2").xAdd("3");

	private void test(final String expected1, final String expected2, final String expected3, final String separator, final BiFunction<String, Integer, String> converter) {
		Assert.assertEquals(expected1, KListUtils.join(this.list1, separator, converter));
		Assert.assertEquals(expected2, KListUtils.join(this.list2, separator, converter));
		Assert.assertEquals(expected3, KListUtils.join(this.list3, separator, converter));
	}

	private BiFunction<String, Integer, String> converter = (str, index) -> { return "?"; };
	private BiFunction<String, Integer, String> c1 = (str, idx) -> { return "<" + str; };
	private BiFunction<String, Integer, String> c2 = (str, idx) -> { return str + ">"; };
	private BiFunction<String, Integer, String> c3 = (str, idx) -> { return "<" + str + ">"; };

	@Test
	public void test() {
		this.test("1", "1,2", "1,2,3", null, null);
		this.test("1", "1|2", "1|2|3", "|", null);

		this.test("<1", "<1,<2", "<1,<2,<3", null, this.c1);
		this.test("<1", "<1|<2", "<1|<2|<3", "|", this.c1);

		this.test("1>", "1>,2>", "1>,2>,3>", null, this.c2);
		this.test("1>", "1>|2>", "1>|2>|3>", "|", this.c2);

		this.test("<1>", "<1>,<2>", "<1>,<2>,<3>", null, this.c3);
		this.test("<1>", "<1>|<2>", "<1>|<2>|<3>", "|", this.c3);

		this.test("?", "?,?", "?,?,?", null, this.converter);
		this.test("?", "?|?", "?|?|?", "|", this.converter);
	}

}
